!-----------------------------------------------------------------------
!  Copyright (c) 2020 Feasa Enterprises Ltd.
!  Rev 3.0  August 2020
!
!  All Rights Reserved.  Reproduction, adaptation, or translation
!  without prior written permission is prohibited, except as allowed
!  under the copyright laws.
!
!-----------------------------------------------------------------------
!
! Device        : Feasa_20i
! Manufacturer  : Feasa Enterprises Ltd.
! Description   : 20 Channel device to interrogate a LED
! Package       : 20 pin
! Test Platform : 3070
! Safeguard     : cmos_3v3
!
!-----------------------------------------------------------------------
! WARNING:  This is a Led Analyser function referenced by a PDL.
!
!   This test is not intended to be used directly for a Led analyser.
!   Instead, a part description library for the Led analyser should be
!   used where the part description language of that file refers to
!   this file as the part number of one of it's several "sub-components" .
!   Notice also that this test uses signal names rather than ordinal
!   pin numbers.  The referencing part description library should map
!   the device's actual pin numbers to these signal names.
!
!
! Additional Comments:
!
!   This test outputs the 3 test times as part of the sequence capture function
!
!-----------------------------------------------------------------------
!
!*******************************************************************************
!      Synchronous Serial Model for the Feasa LED Analyser
!
!Note: All commands sent to the Led Analyser use the following format
! To send a command
!       - Set Ser_in to MSB
!       - Set Ser_clk to [1]
!       - Set Ser_clk to [0]
!       - Set Ser_in to Bit 6
!       - Set Ser_clk to [1]
!       - Set Ser_clk to [0]
!       - Continue until all 8 bits have been clocked into the Led Analyser

! To read a byte
!       - Set Ser_clk to [1]
!       - Set Ser_clk to [0]
!       - Read Ser_out to MSB
!       - Set Ser_clk to [1]
!       - Set Ser_clk to [0]
!       - Read Ser_out Bit 6
!       - Continue until all 8 bits have been read from the Led Analyser
!
! - Reset the Synchronous serial communications by
!       - Set Addr_0 to [0]
!       - Set Ser_in     to [1]
!       - Execute Ser_clk [1] then Ser_clk[0] at leaset 8 times

! - Send the command (0X92) to read 3 times (off-on-off) for Intensity for a Fiber
! - Send the Fiber number and receive 3 test times (2 bytes each for each time)
!  The 1st Test Time represents the time in msec the LED is off st the beginning!of the sweep
!  The 2nd Test Time represents the time in msec the LED is on
!  The 3rd Test Time represents the time in msec the LED is off at the end of
!  the sweep

test digital; Fibernum(*),Data(*)
sequential

test time 2000m

vector cycle  20u
receive delay 19u

set terminators to on
assign Ser_out            to pins "Ser_out"
assign Ser_in             to pins "Ser_in"

assign Ser_clk            to pins "Ser_clk"

assign GND1               to pins "GND1"

assign Reset              to pins "Reset"

assign OE_bar             to pins "OE_bar"
assign Ext_trig           to pins "Ext_trig"

assign PWM                to pins "PWM"

assign Addr_0             to pins "Addr_0"
assign Addr_1             to pins "Addr_1"
assign Addr_2             to pins "Addr_2"
assign Addr_3             to pins "Addr_3"
assign Addr_4             to pins "Addr_4"

assign WE_bar             to pins "WE_bar"
assign Int                to pins "Int"
assign Color              to pins "Color"
assign Sat                to pins "Sat"

assign RyBy_bar           to pins "RyBy_bar"

assign VCC                to pins "VCC"
assign GND                to pins "GND"

power  VCC, GND, GND1

family   CMOS_3V3

inputs   Ser_in, Ser_clk, Addr_0
outputs  Ser_out, RyBy_bar

nondigital Int, Color, Sat, OE_bar, Ext_trig, PWM
nondigital Addr_1, Addr_2, Addr_3, Addr_4, WE_bar, Reset

!********** assignments **********************

variable Fibernum(*) to groups Ser_in
variable Data(*) to groups Ser_out

capture Ser_out pack 8    !8 data bits in each byte read back from Analyser

!*******************************************************************
!*******************      VECTOR SECTION       *********************
!*******************************************************************

vector  Set_values         !set LED Analyser to known state
   set Addr_0              to "0"
   set Ser_clk             to "0"
   set Ser_in              to "0"
end vector

vector Keep_values        !Save input values.
   set Addr_0              to "k"
   set Ser_clk             to "k"
   set Ser_in              to "k"
end vector


vector Addr_0_high
   initialize to Keep_values
   set Addr_0          to "1"
end vector

vector Addr_0_low
   initialize to Keep_values
   set Addr_0          to "0"
end vector

vector Clock
   initialize to Keep_values
   set Ser_clk          to "t"
end vector

vector Clock_1
   initialize to Keep_values
   set Ser_clk          to "1"
end vector

vector Clock_0
   initialize to Keep_values
   set Ser_clk          to "0"
end vector

vector Start_bit
   initialize to Keep_values
   set Ser_in               to "1"
end vector

vector Ser_in_X
   initialize to Keep_values
   set Ser_in          to "x"
end vector

vector Ser_in_0
   initialize to Keep_values
   set Ser_in          to "0"
end vector

vector In_0
   initialize to Keep_values
   set Ser_in          to "0"
end vector

vector Ser_in_1
   initialize to Keep_values
   set Ser_in          to "1"
end vector

vector In_1
   initialize to Keep_values
   set Ser_in          to "1"
end vector

vector Ser_out_x
   initialize to Keep_values
   set Ser_out         to "x"
end vector

vector Ser_out_0
   initialize to Keep_values
   set Ser_out         to "0"
end vector

vector Out_0
   initialize to Keep_values
   set Ser_out         to "0"
end vector

vector Ser_out_1
   initialize to Keep_values
   set Ser_out         to "1"
end vector

vector Out_1
   initialize to Keep_values
   set Ser_out         to "1"
end vector



!*******************************************************************
!*******************    SUBROUTINE SECTION     *********************
!*******************************************************************

! Command to reset or initialize Communications
! Send 11111111 (FF) to LA
sub Reset_Comms
   execute Addr_0_low              !Chip Select = 0
   execute Clock_0
   execute Ser_in_1                    !1
   execute Clock
   execute Clock
   execute Ser_in_1                    !1
   execute Clock
   execute Clock
   execute Ser_in_1                    !1
   execute Clock
   execute Clock
   execute Ser_in_1                    !1
   execute Clock
   execute Clock
   execute Ser_in_1                    !1
   execute Clock
   execute Clock
   execute Ser_in_1                    !1
   execute Clock
   execute Clock
   execute Ser_in_1                    !1
   execute Clock
   execute Clock
   execute Ser_in_1                    !1
   execute Clock
   execute Clock
end sub

! Command to get the sequence times
! Send 10010010 to LA 0x92
sub Get_sequence_times
   execute Addr_0_high             !Chip Select = 1
   execute Clock_0
   execute Ser_in_1                    !1
   execute Clock
   execute Clock
   execute Ser_in_0                    !0
   execute Clock
   execute Clock
   execute Ser_in_0                    !0
   execute Clock
   execute Clock
   execute Ser_in_1                    !1
   execute Clock
   execute Clock
   execute Ser_in_0                    !0
   execute Clock
   execute Clock
   execute Ser_in_0                    !0
   execute Clock
   execute Clock
   execute Ser_in_1                    !1
   execute Clock
   execute Clock
   execute Ser_in_0                    !0
   execute Clock
   execute Clock
end sub



! Read 8 data bits from Ser_out pin
! The pack 8 statement has been used so the 8 data bits will
! be read back as one Byte
sub Read_Capture
   repeat 8 times
     execute Clock
     execute Clock
     execute Ser_out_x capture   !Captures a bit will be put in array using fetch command

   end repeat
end sub



!*******************************************************************
!*********************      UNIT SECTION       *********************
!*******************************************************************


!-------------------------------------------------------------------
!  Initialize all the inputs to a known value
!-------------------------------------------------------------------
unit "Initialize All"
    execute Set_values
end unit
!-------------------------------------------------------------------



!-------------------------------------------------------------------
!  Clear the input buffer of the Led Analyser to
!  ensure that the led analyser has detected no false
!  clock inputs
!-------------------------------------------------------------------
unit "Reset Communications"
        call Reset_Comms                 !Send Reset Comms Code x0FF
        wait 100u
end unit

unit "Get Sequence times"
      call Get_sequence_times            !Send 0x92 CMD to LA
      wait 20m
end unit

!--------------------------------------------------------------------
!   Unit to Determine the fiber number to read
!--------------------------------------------------------------------------
!   The Fiber number is passed into the test using the Array Fibernum(*)
!   The MSB is passed in first so the format is as follows
!   Fibernum(0) = MSB (Bit7)
!   Fibernum(1) = Bit6
!   Fibernum(2) = Bit6
!   Fibernum(3) = Bit6
!   Fibernum(4) = Bit6
!   Fibernum(5) = Bit6
!   Fibernum(6) = Bit6
!   Fibernum(7) = LSB (Bit0)
! i.e. to read fiber 1 set Fibernum(7) to 1 and all others to 0
!----------------------------------------------------------------------
unit "Fiber No to read"
   execute Ser_in_x drive Fibernum(0)    !MSB
   execute Clock
   execute Clock
   execute Ser_in_x drive Fibernum(1)    !Bit6
   execute Clock
   execute Clock
   execute Ser_in_x drive Fibernum(2)    !Bit5
   execute Clock
   execute Clock
   execute Ser_in_x drive Fibernum(3)    !Bit4
   execute Clock
   execute Clock
   execute Ser_in_x drive Fibernum(4)    !Bit3
   execute Clock
   execute Clock
   execute Ser_in_x drive Fibernum(5)    !Bit2
   execute Clock
   execute Clock
   execute Ser_in_x drive Fibernum(6)    !Bit1
   execute Clock
   execute Clock
   execute Ser_in_x drive Fibernum(7)    !Bit0
   execute Clock
   execute Clock
   wait 20m        !need to wait 20ms as the Led Analyser is performing calculations
end unit

!------------------------------------------------------------
!---------------------------------------------------------------------------
!      Unit to read Int data
!------------------------------------------------------------
! 6 Bytes of data are read back.
! 1,2 = time#1  off time before LED comes on
! 3,4 = time#2  on time for LED
! 5,6 = time#3  off time after LED goes off
! Time1 = Byte#1 * 256 + Byte#2
! Time2 = Byte#3 * 256 + Byte#4
! Time3 = Byte#5 * 256 + Byte#6
!------------------------------------------------------------
unit "Read Back Int"

   repeat 6 times    !read 6 bytes
     call Read_Capture
   end repeat

fetch Data(*)  ! puts measured values in the array

end unit
!------------------------------------------------------------


! End of Test


